package com.briup.jz.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.Address;
import com.briup.jz.bean.AddressExample;
import com.briup.jz.dao.AddressMapper;
import com.briup.jz.service.IAddressService;
import com.briup.jz.utils.CustomerException;

@Service
public class AddressServiceImpl implements IAddressService{
	@Autowired
	private AddressMapper addressMapper;
	
	@Override
	public void saveOrUpdate(Address address) throws CustomerException {
		if(address.getId()!=null) {
			addressMapper.updateByPrimaryKey(address);
		}/*else {
			// 判断是否有同名，如果有抛出异常
			AddressExample example = new AddressExample();
			example.createCriteria().andIdEqualTo(address.getId());
			List<Address> list = addressMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名已存在");
			}*/
		addressMapper.insert(address);
	}

	@Override
	public List<Address> query(Long id) {
		AddressExample example=new AddressExample();
		if(id!=null) {
			example.createCriteria().andIdEqualTo(id);
		}
		List<Address> list = addressMapper.selectByExample(example);
		for(Address a:list) {
			System.out.println(a);
		}
		System.out.println("aaaaaaa");
		return list;
	}
		
	@Override
	public void deleteById(long id) throws CustomerException {
		Address address=addressMapper.selectByPrimaryKey(id);
		if(address==null) {
			throw new CustomerException("要删除不存在");
		}
		addressMapper.deleteByPrimaryKey(id);		
	}

	@Override
	public List<Address> pageQuery(Long id) {
		// TODO Auto-generated method stub
		AddressExample example = new AddressExample();
		return addressMapper.selectByExample(example);
	}

}


